<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\AutoMsgVerfyBad;
use App\Admin\Actions\Post\AutoMsgVerfyOk;
use App\Admin\Actions\Post\ProfileLogVerfyBad;
use App\Admin\Actions\Post\ProfileLogVerfyOk;
use App\Admin\Extensions\ProfileVerify;
use App\Http\Controllers\Controller;
use App\Model\Message;
use App\Model\ProfileVerifyLog;
use App\Model\SettingUserQuestion;
use App\Model\UserProfile;
use App\Model\UserQa;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use Illuminate\Support\FacadesLog;

/**
 * 各种资料审核
 */
class ProfileVerfyController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('个人资料审核');
        $content->body($this->grid());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new ProfileVerifyLog());
        $where = [
            ['content_type', '<>', ProfileVerifyLog::TYPE_REAL_AVATAR],
            ['content_type', '<>', ProfileVerifyLog::TYPE_QA],
            ['content_type', '<>', ProfileVerifyLog::TYPE_POSTER],
            ['content_type', '<>', ProfileVerifyLog::TYPE_AVATAR],
            ['content_type', '<>', ProfileVerifyLog::TYPE_VIDEO],
            ['content_type', '<>', ProfileVerifyLog::TYPE_WECHAT],
        ];
        $grid->model()->where($where)->orderBy('id', 'desc');
        $grid->id('记录ID');
        $grid->content('内容')->display(function ($data) {
            if (!$data) return '';
            switch ($this->content_type) {
                case ProfileVerifyLog::TYPE_POSTER:
                case ProfileVerifyLog::TYPE_AVATAR:
                    return ComUtil::buidlImgaeHtml(explode(',', $data));
                case ProfileVerifyLog::TYPE_VIDEO:
                    return ComUtil::buildAudio($data, 'video');
                case ProfileVerifyLog::TYPE_VOICE:
                    return ComUtil::buildAudio($data, 'audio');
                case ProfileVerifyLog::TYPE_QA:
                    $info = json_decode($data, true);
                    $question = SettingUserQuestion::find($info['question_id']);
                    return !$question ? "-" : "问题：{$question->question}<br/>回答：{$info['answer']}";
                case ProfileVerifyLog::TYPE_WECHAT:
                    $info = json_decode($data, true);
                    return ComUtil::buidlImgaeHtml($info['qrcode']) . "（微信账号：{$info['account']}）";
            }
            return $data;
        });
        $grid->content_type('类型')->display(function ($data) {
            return ProfileVerifyLog::TYPE[$data] ?? '未知类型';
        });
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->gender('性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->userProfile()->nick_name('用户昵称');
        $grid->created_at('提交时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->status('状态')->display(function ($data) {
            return ProfileVerifyLog::STATUS[$data];
        });

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        // $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();

            $filter->column(1 / 3, function ($filter) {
                $filter->equal('content_type', '类型')->select(ProfileVerifyLog::TYPE);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('status', '状态')->select(ProfileVerifyLog::STATUS);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new ProfileVerify($actions->getKey(), $actions->row, 'profile/verify'));
        });

        $grid->batchActions(function ($batch) {
            $batch->add(new ProfileLogVerfyOk());
            $batch->add(new ProfileLogVerfyBad());
        });
        return $grid;
    }

    public function index_poster()
    {
        $content = new Content();
        $content->header('照片墙审核');
        $content->body($this->grid_poster());
        return $content;
    }

    protected function grid_poster()
    {
        $grid = new Grid(new ProfileVerifyLog());
        $types = [ProfileVerifyLog::TYPE_POSTER, ProfileVerifyLog::TYPE_VIDEO];
        $grid->model()->whereIn('content_type', $types)->orderBy('id', 'desc');
        $grid->id('记录ID');
        $grid->content('内容')->display(function ($data) {
            if (!$data) return '';
            switch ($this->content_type) {
                case ProfileVerifyLog::TYPE_POSTER:
                case ProfileVerifyLog::TYPE_AVATAR:
                    return ComUtil::buidlImgaeHtml(explode(',', $data));
                case ProfileVerifyLog::TYPE_VIDEO:
                    return ComUtil::buildAudio($data, 'video');
            }
            return $data;
        });
        $grid->content_type('类型')->display(function ($data) {
            return ProfileVerifyLog::TYPE[$data] ?? '未知类型';
        });
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->gender('性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->userProfile()->nick_name('用户昵称');
        $grid->created_at('提交时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->status('状态')->display(function ($data) {
            return ProfileVerifyLog::STATUS[$data];
        });

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        // $grid->disableRowSelector();
        $grid->filter(function ($filter) use ($types) {
            $filter->expand();
            $filter->disableIdFilter();

            $filter->column(1 / 3, function ($filter) use ($types) {
                $typeInfo = [];
                foreach ($types as $type) $typeInfo[$type] = ProfileVerifyLog::TYPE[$type];
                $filter->equal('content_type', '类型')->select($typeInfo);
                $filter->equal('userProfile.gender', '性别')->select([1 => '男', 0 => '女']);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('status', '状态')->select(ProfileVerifyLog::STATUS);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new ProfileVerify($actions->getKey(), $actions->row, 'profile/verify'));
        });

        $grid->batchActions(function ($batch) {
            $batch->add(new ProfileLogVerfyOk());
            $batch->add(new ProfileLogVerfyBad());
        });
        return $grid;
    }

    function verify()
    {
        $id = request('id');
        $status = intval(request('status'));
        $record = ProfileVerifyLog::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        switch ($status) {
            case ProfileVerifyLog::STATUS_BAD:
                ProfileVerifyLog::verfyBad($record);
                break;
            case ProfileVerifyLog::STATUS_OK:
                ProfileVerifyLog::verfyOk($record);
                break;
        }
        return ['ret' => 0, 'message' => '操作成功！'];
    }


    public function index_wechat()
    {
        $content = new Content();
        $content->header('微信认证审核');
        $content->body($this->grid_wechat());
        return $content;
    }

    protected function grid_wechat()
    {
        $grid = new Grid(new ProfileVerifyLog());
        $where = [
            ['content_type', ProfileVerifyLog::TYPE_WECHAT]
        ];
        $grid->model()->where($where)->orderBy('id', 'desc');
        $grid->id('记录ID');
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->gender('性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->userProfile()->role('角色')->display(function ($role) {
            return UserProfile::ROLE_HOST == $role ? '女用户' : "用户";
        });
        $grid->userProfile()->nick_name('用户昵称');
        $grid->wechat_qrcode('微信二维码')->display(function ($data) {
            if (!$this->content) return '';
            $info = json_decode($this->content, true);
            return ComUtil::buidlImgaeHtml($info['qrcode']);
        });
        $grid->wechat_account('微信账号')->display(function ($data) {
            if (!$this->content) return '';
            $info = json_decode($this->content, true);
            return $info['account'];
        });
        $grid->created_at('提交时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->status('状态')->display(function ($data) {
            return ProfileVerifyLog::STATUS[$data];
        });

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('status', '状态')->select(ProfileVerifyLog::STATUS);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new ProfileVerify($actions->getKey(), $actions->row, 'profile/verify'));
        });
        return $grid;
    }
}
